﻿using aier.business.logic;
using aier.dataaccess.model;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
    //[Authorize]
    /// <summary>
    /// 筛查回访
    /// </summary>
    [RoutePrefix("api/ScreenCallBack")]
    public class ScreenCallBackController : ApiController
    {
        /// <summary>
        /// 日志
        /// </summary>
        public  ILog log { get; set; }
        /// <summary>
        /// 回访逻辑层
        /// </summary>
        public ScreenCallBackBll screenCallBackBll { get; set; }

        /// <summary>
        /// 工具类
        /// </summary>
        public ApiTool apiTool { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public IHttpActionResult Get(string accountId) {
            try
            {
                if (!string.IsNullOrWhiteSpace(accountId))
                {
                    var callBack = screenCallBackBll.GetScreenCallBacksByHospID(new Guid(accountId));
                    return Ok(callBack.ToList());
                }

                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
      
            }
        }

        /// <summary>
        /// 创建筛查回访信息----回访详情
        /// </summary>
        /// <param name="screenCallBack">筛查回访对像</param>
        /// <returns></returns>
        [Authorize]
        [ActionName("CreateCallBackResult")]
        public IHttpActionResult CreateCallBackResult(ScreenCallBackModel screenCallBack)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                var ownerId = userInfo.userid;
                Guid sreenCallbackId = screenCallBackBll.CreateScreenCallBack(ownerId,screenCallBack);
                return Ok(sreenCallbackId);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }


        /// <summary>
        /// 根据任务ID与筛查宣教ID获取筛查回访结果
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="propagandaId"></param>
        /// <returns></returns>
        [Authorize]
        [ActionName("GetScreenCallBackResult")]
        public IHttpActionResult GetScreenCallBackResult(Guid taskId, Guid screenId)
        {
            try
            {
                if (taskId!=Guid.Empty && screenId != Guid.Empty)
                {
                    var callBack = screenCallBackBll.GetScreenCallBacksByID(taskId, screenId);
                    return Ok(callBack);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 修改筛查回访信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [ActionName("UpdateScreenCallBacksData")]
        public IHttpActionResult UpdateScreenCallBacksData(ScreenCallBackModel screenCallBackModel)
        {
            try
            {
                if (screenCallBackModel.new_screencallbackid != Guid.Empty
                    &&(!string.IsNullOrWhiteSpace(screenCallBackModel.new_description) 
                    || screenCallBackModel.new_communicateresult != null 
                    || screenCallBackModel.new_contactresult != null))
                {
                    UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                    var ownerId = userInfo.userid;
                    var callBack = screenCallBackBll.UpdateScreenCallBacksData(ownerId, screenCallBackModel);

                    //if(screenCallBackModel.)
                    return Ok(callBack);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        [Authorize]
        [HttpPost]
        [ActionName("UpdateScreenCallBacksDelay")]
        public IHttpActionResult UpdateScreenCallBacksDelay([FromBody]ScreenCallBackModel screenCallBackModel,[FromUri]int delayDay)
        {
            try
            {
                if (screenCallBackModel.new_task != Guid.Empty)
                {
                    UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                    var result =  screenCallBackBll.UpdateScreenCallBacksDelay(userInfo.userid, screenCallBackModel, delayDay);
                    return Ok(result);
                }
                
                return new ApiErrorResult("new_task 为空", Request);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据任务ID与筛查宣教ID获取筛查回访结果
        /// </summary>
        /// <returns></returns>
        [ActionName("GetScreenCallBackByLeadId")]
        public IHttpActionResult GetScreenCallBackByLeadId(Guid leadId)
        {
            try
            {
                if (leadId != Guid.Empty)
                {
                    var data = screenCallBackBll.GetScreenCallBackByLeadId(leadId);
                    return Ok(data);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }
    }
}
